package com.tian.web.admin.action;

import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.tian.core.action.BaseAction;
import com.tian.core.entity.Log;
import com.tian.core.entity.Master;
import com.tian.core.entity.Operbutton;
import com.tian.core.util.EscapeUnescape;
import com.tian.web.admin.service.LogService;
import com.tian.web.admin.service.MasterService;
import com.tian.web.admin.service.OperbuttonService;

@Namespace("/admin")
@Results( {  
	@Result(type = "dispatcher", name = "main", location = "/pages/admin/main.jsp"),
	@Result(type = "dispatcher", name = "login", location = "/pages/admin/login.jsp"),
	@Result(type = "dispatcher",name = "input", location = "/pages/admin/login.jsp")
})
public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Master master;
	private String valid;
	@Resource
	private MasterService masterService;
	@Resource
	private OperbuttonService buttonService;
	@Resource
	private LogService logService;
	
	@Override
	public String execute() throws Exception {
/*		List<Master> list = masterService.getAll(Master.class);
		master = list.get(0);
		System.out.println(master.getName());*/
		return "login";
	}
	
	public String login()throws Exception {
		//System.out.println(master.getName());
		Master m = masterService.get("name", master.getName());
		
		Cookie cookie=new Cookie("loginName",EscapeUnescape.escape(m.getName()));
		cookie.setMaxAge(1000*60*60*24*365);//有效时间为一年
		ServletActionContext.getResponse().addCookie(cookie);
		
		if(null != m){
			m.getMastergroups();
			if(!m.getPassword().equals(master.getPassword())){
				addActionError("密码不正确！");
				return "input";
			}else
			{
				HttpSession session = ServletActionContext.getRequest().getSession();
				session.setAttribute("user", m);
				String ip = ServletActionContext.getRequest().getRemoteAddr();
				Log log = new Log(m.getId(),m.getName(),new Date(),"登录","登录",ip);
				logService.save(log);
				return "main";
			}
		}else{
			addActionError("用户名不存在！");
			return "input";
		}
	}
	
	public String logout()throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		master = (Master)session.getAttribute("user");
		if(master != null){
			String ip = ServletActionContext.getRequest().getRemoteAddr();
			Log log = new Log(master.getId(),master.getName(),new Date(),"退出","退出",ip);
			session.removeAttribute("user");
		}
		
		return execute();
	}

	public Master getMaster() {
		return master;
	}

	public void setMaster(Master master) {
		this.master = master;
	}

	public String getValid() {
		return valid;
	}

	public void setValid(String valid) {
		this.valid = valid;
	}
	
}
